using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dapper;
using EP.Base.BaseService.Manager.Config;
using EP.Base.BaseService.Manager.Dal.Helper;
using EP.Base.BaseService.Manager.Interface.Data;
using EP.Base.BaseService.Manager.Model.Entity;

namespace EP.Base.BaseService.Manager.Dal
{    
	/// <summary>
    /// IndexConfig 
    /// </summary>
    public class IndexConfigs:IIndexConfigs
    {


        /// <summary>
        /// 添加IndexConfig
        /// </summary>
        /// <param name="indexconfig"></param>
        /// <returns></returns>
        public int Add(IndexConfig indexconfig)
        {
            string sql = @"INSERT INTO [IndexConfig]
			([ClusterId],[IndexNamePrefix],[IndexNameSuffix],[Alias],[Explain],[StorageMode],[Users],[Source],[NeedBackup],[RetainTimeRange],[UseState],[CreateTime])
			VALUES
			(@ClusterId,@IndexNamePrefix,@IndexNameSuffix,@Alias,@Explain,@StorageMode,@Users,@Source,@NeedBackup,@RetainTimeRange,@UseState,@CreateTime) 
			SELECT SCOPE_IDENTITY()
			";
            var par = new DynamicParameters();
            par.Add("@ClusterId", indexconfig.ClusterId, DbType.Int32);
            par.Add("@IndexNamePrefix", indexconfig.IndexNamePrefix, DbType.AnsiString);
            par.Add("@IndexNameSuffix", indexconfig.IndexNameSuffix, DbType.AnsiString);
            par.Add("@Alias", indexconfig.Alias, DbType.AnsiString);
            par.Add("@Explain", indexconfig.Explain, DbType.String);
            par.Add("@StorageMode", indexconfig.StorageMode, DbType.Int16);
            par.Add("@Users", indexconfig.Users, DbType.String);
            par.Add("@Source", indexconfig.Source, DbType.String);
            par.Add("@NeedBackup", indexconfig.NeedBackup, DbType.Boolean);
            par.Add("@RetainTimeRange", indexconfig.RetainTimeRange, DbType.String);
            par.Add("@UseState", indexconfig.UseState, DbType.AnsiString);
            par.Add("@CreateTime", indexconfig.CreateTime, DbType.DateTime);
            return DapWrapper.InnerQueryScalarSql<int>(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
        }
        /// <summary>
        /// 修改IndexConfig
        /// </summary>
        /// <param name="indexconfig"></param>
        /// <returns></returns>
        public int Update(IndexConfig indexconfig)
        {
            string sql = @"UPDATE  [IndexConfig] SET 
						ClusterId=@ClusterId,
						IndexNamePrefix=@IndexNamePrefix,
						IndexNameSuffix=@IndexNameSuffix,
						Alias=@Alias,
						Explain=@Explain,
						StorageMode=@StorageMode,
						Users=@Users,
						Source=@Source,
						NeedBackup=@NeedBackup,
						RetainTimeRange=@RetainTimeRange,
						UseState=@UseState,
						CreateTime=@CreateTime
 WHERE Id=@Id";
            var par = new DynamicParameters();
            par.Add("@Id", indexconfig.Id, DbType.Int32);
            par.Add("@ClusterId", indexconfig.ClusterId, DbType.Int32);
            par.Add("@IndexNamePrefix", indexconfig.IndexNamePrefix, DbType.AnsiString);
            par.Add("@IndexNameSuffix", indexconfig.IndexNameSuffix, DbType.AnsiString);
            par.Add("@Alias", indexconfig.Alias, DbType.AnsiString);
            par.Add("@Explain", indexconfig.Explain, DbType.String);
            par.Add("@StorageMode", indexconfig.StorageMode, DbType.Int16);
            par.Add("@Users", indexconfig.Users, DbType.String);
            par.Add("@Source", indexconfig.Source, DbType.String);
            par.Add("@NeedBackup", indexconfig.NeedBackup, DbType.Boolean);
            par.Add("@RetainTimeRange", indexconfig.RetainTimeRange, DbType.String);
            par.Add("@UseState", indexconfig.UseState, DbType.AnsiString);
            par.Add("@CreateTime", indexconfig.CreateTime, DbType.DateTime);
            return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
        }
        /// <summary>
        /// 根据id获取IndexConfig
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IndexConfig Get(int id)
        {
            string sql = "select [Id],[ClusterId],[IndexNamePrefix],[IndexNameSuffix],[Alias],[Explain],[StorageMode],[Users],[Source],[NeedBackup],[RetainTimeRange],[UseState],[CreateTime] FROM [IndexConfig] WHERE Id=@Id";
            var par = new DynamicParameters();
            par.Add("@Id", id, DbType.Int32);
            return DapWrapper.InnerQuerySql<IndexConfig>(DbConfig.EpBaseServiceManagerConnectionString, sql, par).FirstOrDefault();
        }
        /// <summary>
        /// 根据id删除IndexConfig
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Int32 Delete(int id)
        {
            string sql = "DELETE [IndexConfig] WHERE Id=@Id";
            var par = new DynamicParameters();
            par.Add("@Id", id, DbType.Int32);
            return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
        }
        /// <summary>
        /// 根据ids删除IndexConfig多条记录
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public Int32 Deletes(int[] ids)
        {
            if (ids.Length == 0)
                return 0;
            string sql = "DELETE [IndexConfig] WHERE Id IN (" + string.Join(",", ids) + ")";
            return DapWrapper.InnerExecuteText(DbConfig.EpBaseServiceManagerConnectionString, sql);
        }
        /// <summary>
        /// 获取IndexConfig分页列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="beginTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="recordCount">总记录数</param>
        /// <returns>IndexConfig列表</returns>
        public List<IndexConfig> GetPageList(int pageIndex, int pageSize, DateTime? beginTime, DateTime? endTime, out int recordCount)
        {
            recordCount = 0;
            var par = new DynamicParameters();
            par.Add("@PageIndex", pageIndex, DbType.Int32);
            par.Add("@PageSize", pageSize, DbType.Int32);
            par.Add("@BeginTime", beginTime, DbType.DateTime);
            par.Add("@EndTime", !endTime.HasValue ? endTime : endTime.Value.AddDays(1).AddMilliseconds(-1), DbType.DateTime);
            par.Add("@TotalCount", recordCount, DbType.Int32, ParameterDirection.Output);
            var result = DapWrapper.InnerQueryProc<IndexConfig>(DbConfig.EpBaseServiceManagerConnectionString, "proc_GetIndexConfigPageList", par);
            recordCount = par.Get<int>("@TotalCount");
            return result;
        }
        /// <summary>
        /// 获取所有IndexConfig
        /// </summary>
        /// <returns></returns>
        public List<IndexConfig> GetAll()
        {
            string sql = "select [Id],[ClusterId],[IndexNamePrefix],[IndexNameSuffix],[Alias],[Explain],[StorageMode],[Users],[Source],[NeedBackup],[RetainTimeRange],[UseState],[CreateTime] FROM [IndexConfig] WHERE Id=@Id";

            return DapWrapper.InnerQuerySql<IndexConfig>(DbConfig.EpBaseServiceManagerConnectionString, sql);
        }
    }
}
